package com.wntms.ft.dao;

import com.wntms.ft.pojo.CarriageContract;
import com.wntms.ft.pojo.CustomerBill;
import com.wntms.ft.pojo.DriverBill;
import com.wntms.ft.pojo.FreightContractBill;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 票据管理
 */
@Mapper
public interface BillDao {
    /**
     * 司机回执票据查询
     * @return
     */
    @Select("SELECT db.id,di.name, db.cid,db.deleted FROM driver_bill AS db,\n" +
            "\tdriver_information AS di\n" +
            "\tWHERE db.diid = di.id")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "cid",property = "number"),
            @Result(column = "deleted",property = "isDeleted"),
    })
    public List<DriverBill> findAllDriver();


    /**
     * 删除司机回执单信息
     * @param number
     * @return
     */
    @Update("update driver_bill set deleted = 0 where id=#{number}")
    public int deleteDriver(int number);

    /**
     * 查询所有客户回执表信息
     * @return
     */
    @Select("SELECT * FROM customer_accept")
    @Results({
            @Result(column = "name",property = "name"),
            @Result(column = "fid",property = "number"),
            @Result(column = "deleted",property = "isDeleted"),
    })
   public List<CustomerBill> findAllCustomer();

    /**
     * 根据用户编号删除用户回执单
     * @param number
     * @return
     *
     */
    @Update("update customer_accept set deleted = 0 where fid=#{number}")
    public int deleteCustomer(String number);


    /**
     * 查询所有的货运单信息
     * @return
     */
    @Select("SELECT fc.id,fc.fid,fc.fare,fc.insurancefare,fc.pay,fc.sdate, ci.name as cname,cu.name,fc.deleted FROM freight_contract AS fc,\n" +
            "\t      customer_information AS ci,\n" +
            "\t      customer_in AS cu\n" +
            "\t      WHERE fc.uid = ci.id\n" +
            "\t      AND fc.coid = cu.id")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "fid",property = "fid"),
            @Result(column = "fare",property = "fare"),
            @Result(column = "insurancefare",property = "insuranceFare"),
            @Result(column = "pay",property = "pay"),
            @Result(column = "sdate",property = "sDate"),
            @Result(column = "cname",property = "dName"),
            @Result(column = "name",property = "sName"),
            @Result(column = "deleted",property = "isDeleted"),
    })
    public List<FreightContractBill> findAllFreightBill();


    /**
     * 删除货运单合同表
     * @param id
     * @return
     */
    @Update("update freight_contract set deleted = 0 where id=#{number}")
    public int deleteByFreightId(int id);



    @Select("SELECT cc.cid,cc.fid,ci.name AS cname,\n" +
            "cc.receive_address,cc.shipping_time,\n" +
            "cc.arrival_time,cui.name AS cuname,cc.insurance,cc.fare,\n" +
            "di.name AS dname,cc.deleted\n" +
            " FROM carriage_contract AS cc,\n" +
            "\tcustomer_information AS ci,\n" +
            "\tcustomer_in AS cui,\n" +
            "\tdriver_information AS di\n" +
            "\t WHERE cc.senderid = ci.id\n" +
            "\tAND cc.receiverid = cui.id\n" +
            "\t AND cc.diid = di.id")
    @Results({
            @Result(column = "cid",property = "cid"),
            @Result(column = "fid",property = "fid"),
            @Result(column = "cname",property = "sName"),
            @Result(column = "receive_address",property = "receiveAddress"),
            @Result(column = "shipping_time",property = "shippingTime"),
            @Result(column = "arrival_time",property = "arrivalTime"),
            @Result(column = "cuname",property = "rName"),
            @Result(column = "insurance",property = "insurance"),
            @Result(column = "fare",property = "fare"),
            @Result(column = "dname",property = "dName"),
            @Result(column = "deleted",property = "isDeleted"),
    })
    public List<CarriageContract> findAllCarriageBill();


    @Update("update carriage_contract set deleted = 0 where cid=#{number}")
    public int deleteByCarriageId(String id);

}
